package com.lion.common.spring.openapi;

import com.lion.common.spring.openapi.customizers.openapi.GroupFilterOpenApiCustomizer;
import com.lion.common.spring.openapi.customizers.openapi.TagPrefixOpenApiCustomizer;
import com.lion.common.spring.openapi.customizers.openapi.TagSortingOpenApiCustomizer;
import com.lion.common.spring.openapi.customizers.operation.GroupMarkOperationCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.models.GroupedOpenApi;

/**
 * @author Chris Chan
 * Create On 2025/8/23 下午6:55
 * Use for: 分组OpenApi配置
 * Explain:
 */
public interface BaseGroupedOpenApiConfiguration {
    Logger logger = LoggerFactory.getLogger(BaseGroupedOpenApiConfiguration.class);

    /**
     * 构建分组OpenApi
     *
     * @param order       分组顺序
     * @param group       分组名称
     * @param displayName 分组显示名称
     * @return
     */
    default GroupedOpenApi build(int order, String group, String displayName) {
        return GroupedOpenApi.builder()
                .group(formatGroup(order, group))
                .displayName(formatDisplayName(order, displayName))
                .pathsToMatch("/**")
                .addOperationCustomizer(GroupMarkOperationCustomizer.of(group))
                .addOpenApiCustomizer(GroupFilterOpenApiCustomizer.of(group))
                .addOpenApiCustomizer(new TagSortingOpenApiCustomizer())
                .addOpenApiCustomizer(new TagPrefixOpenApiCustomizer())
                .build();
    }

    /**
     * 构建分组OpenApi
     *
     * @param order          分组顺序
     * @param group          分组名称
     * @param displayName    分组显示名称
     * @param packagesToScan 要扫描的包路径
     * @return
     */
    default GroupedOpenApi build(int order, String group, String displayName, String... packagesToScan) {
        return GroupedOpenApi.builder()
                .group(formatGroup(order, group))
                .displayName(formatDisplayName(order, displayName))
                .packagesToScan(packagesToScan)
                .addOperationCustomizer(GroupMarkOperationCustomizer.of(group))
                .addOpenApiCustomizer(GroupFilterOpenApiCustomizer.of(group))
                // 在这里添加用于控制顺序，保证GroupFilterOpenApiCustomizer先执行
                .addOpenApiCustomizer(new TagSortingOpenApiCustomizer())
                .addOpenApiCustomizer(new TagPrefixOpenApiCustomizer())
                .build();
    }

    /**
     * 格式化分组顺序为两位数，不足补零
     *
     * @param order 分组顺序
     * @return 格式化后的分组顺序
     */
    default String formatOrder(int order) {
        return String.format("%02d", order);
    }

    /**
     * 格式化分组名称
     *
     * @param order 分组顺序
     * @param name  分组名称
     * @return 格式化后的分组名称
     */
    default String formatGroup(int order, String name) {
        return formatOrder(order) + "." + name;
    }

    /**
     * 格式化分组显示名称
     *
     * @param order 分组顺序
     * @param name  分组名称
     * @return 格式化后的分组显示名称
     */
    default String formatDisplayName(int order, String name) {
        return "G" + formatGroup(order, name);
    }

}
